﻿using RLRT.Common;
using RLRT.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RLRT.Web
{
    public partial class WarehouseEdit : LocalDialog
    {
        #region 属性

        /// <summary>
        /// 当前ID
        /// </summary>
        private string CurrentID
        {
            get
            {
                object tempObject = ViewState["CurrentID"];
                return (tempObject != null) ? (string)tempObject : string.Empty;
            }
            set
            {
                ViewState["CurrentID"] = value;
            }
        }

        #endregion

        /// <summary>
        /// 页面加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                #region 验证权限

                #endregion

                #region 获取参数

                string id = Request.QueryString["ID"];
                if (id != null && id.Trim().Length != 0)
                {
                    this.CurrentID = id.Trim();
                }

                #endregion

                //初始化
                this.Initialize();
            }

            #region 页面标题

            this.Title = PageTitle.Text;

            #endregion

            #region 页面要素

            this.AssignDialogCallback(CancelButton, "'Close'", "window");

            #endregion
        }

        #region 初始化

        /// <summary>
        /// 初始化
        /// </summary>
        private void Initialize()
        {
            #region 页面内容

            using (RTProvider rtProvider = new RTProvider())
            {
                var prop = rtProvider.GetBaseParam("WH", "");
                LocalGlobal.BindListItems(Property, prop.DefaultView, "Description", "Description", true);
            }

            #endregion

            if (this.CurrentID.Length == 0)
            {
                //新增
            }
            else
            {
                //编辑
                this.BindItem();
            }
        }

        #endregion

        #region 绑定数据

        /// <summary>
        /// 绑定项目
        /// </summary>
        private void BindItem()
        {
            using (RTProvider rtProvider = new RTProvider())
            {
                DataRow row = rtProvider.GetWarehouse(this.CurrentID);
                if (row != null)
                {
                    wmwhs.Text = row["wmwhs"].ToString();
                    wmdes.Text = row["wmdes"].ToString();
                    area.Text = row["wmarea"].ToString();
                    prov.Text = row["wmprov"].ToString();
                    city.Text = row["wmcoty"].ToString();
                    adr1.Text = row["wmadr1"].ToString();
                    adr2.Text = row["wmadr2"].ToString();
                    adr3.Text = row["wmadr3"].ToString();
                    pon1.Text = row["wmpon1"].ToString();
                    pon2.Text = row["wmpon2"].ToString();
                    pon3.Text = row["wmpon3"].ToString();
                    fax1.Text = row["wmfax1"].ToString();
                    fax2.Text = row["wmfax2"].ToString();
                    emai1.Text = row["wmmal1"].ToString();
                    emai2.Text = row["wmmal2"].ToString();
                    Contact.Text = row["wmctat"].ToString();
                    Property.SelectedValue = row["wmtyp"].ToString().Trim();
                    Company.Text = row["wmranh"].ToString();
                }

            }
        }

        #endregion

        #region 操作

        /// <summary>
        /// 确定
        /// </summary>
        protected void OKButton_Click(object sender, EventArgs e)
        {
            using (RTProvider rtProvider = new RTProvider())
            {
                if (this.CurrentID.Length == 0)
                {
                    //新增
                    try
                    {
                        DataRow row = rtProvider.GetWarehouse(wmwhs.Text.Trim());
                        if (row != null)
                        {
                            this.ShowErrorMessage("这个仓库记录是存在的，请确认后重新输入新的内容");
                            return;
                        }

                        rtProvider.InsertWarehouse(wmwhs.Text, wmdes.Text, area.Text, prov.Text, city.Text, adr1.Text, adr2.Text, adr3.Text, pon1.Text, pon2.Text, pon3.Text, fax1.Text, fax2.Text, emai1.Text, emai2.Text, Contact.Text, Property.SelectedValue, Company.Text);
                    }
                    catch (Exception error)
                    {
                        this.ShowErrorMessage(this.GetGlobalResourceString("CreateErrorMessage") + error.Message);
                        return;
                    }
                }
                else
                {
                    //编辑
                    try
                    {
                        rtProvider.UpdateWarehouse(this.CurrentID, wmdes.Text, area.Text, prov.Text, city.Text, adr1.Text, adr2.Text, adr3.Text, pon1.Text, pon2.Text, pon3.Text, fax1.Text, fax2.Text, emai1.Text, emai2.Text, Contact.Text, Property.SelectedValue, Company.Text);
                    }
                    catch (Exception error)
                    {
                        this.ShowErrorMessage(this.GetGlobalResourceString("UpdateErrorMessage") + error.Message);
                        return;
                    }
                }
            }
            //回调
            this.DialogCallback("'CloseRefresh'", "window");
        }

        #endregion
    }
}